package airAgent.group2.util;

import jade.core.AID;
import java.io.PrintStream;
import ontology.ontology.Direction;
import ontology.ontology.Ticket;

public class TicketManagement
{
	private static final int totalDays = 31;
    public DailyTicketData monthlyTicketData[];
    
    private class Flight
    {
    	private static final int totalSeats = 5;
        private Seat seats[];

        public int getFirstFreeSeat()
        {
            for(int i = 0; i < totalSeats; i++)
                if(seats[i].client ==null)
                    return i;

            return -1;
        }

        public int getTotalOfFreeSeats()
        {
            int total = 0;
            for(int i = 0; i < totalSeats; i++)
                if(seats[i].client == null)
                    total++;

            return total;
        }

        public int getNumberOfSeatsSoldToClient(AID client)
        {
            int total = 0;
            for(int i = 0; i < totalSeats; i++){
                if( seats[i].client != null ){
	            	if(seats[i].client.equals(client))
	                    total++;
                }
            }
            return total;
        }

        public void cancelAllSeatsSoldToClient(AID client)
        {
            try {
				for (int i = 0; i < totalSeats; i++) {
					if (seats[i].client != null) {
						if (seats[i].client.equals(client))
							seats[i].client = null;
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
        }

        public Flight()
        {
            seats = new Seat[totalSeats];
            for(int i = 0; i < totalSeats; i++){
                seats[i] = new Seat();
            }

        }
    }

    private class DailyTicketData
    {
        public Flight getFlight(boolean direction)
        {
            if(direction)
                return toMontegancedo;
            else
                return toBarajas;
        }

        private Flight toMontegancedo;
        private Flight toBarajas;


        public DailyTicketData()
        {
            toMontegancedo = new Flight();
            toBarajas = new Flight();
        }
    }

    private class Seat
    {
    	private AID client;
    	  
        public Seat()
        {
            client = null;
        }
      
    }

    public TicketManagement()
    {
        monthlyTicketData = new DailyTicketData[totalDays];
        for(int i = 0; i < totalDays; i++)
            monthlyTicketData[i] = new DailyTicketData();

    }
    
    public int getTotalNumberOfAvailableSeats(Ticket ticket){
    	return monthlyTicketData[ticket.getTicketDate()].getFlight(ticket.getTicketDirection().getValue()).getTotalOfFreeSeats();   
    }
    
    public int getTotalNumberOfSoldTicketsForDay(int day){
    	
    	return (5 - monthlyTicketData[day].getFlight(true).getTotalOfFreeSeats()) + (5 - monthlyTicketData[day].getFlight(false).getTotalOfFreeSeats());  
    	   
    }
    
    public int getTotalNumberOfSoldTicketsForDayForDirection(int day, boolean dir){
    	
    	return (5 - monthlyTicketData[day].getFlight(dir).getTotalOfFreeSeats());  
    	   
    }

    public boolean sellTicket(AID client, Ticket ticket)
    {
    	int seatNumber=-1;
        int numberOfSeats = ticket.getTicketSeats();
        int date = ticket.getTicketDate();
        boolean direction = ticket.getTicketDirection().getValue();
        for(int i = 0; i < numberOfSeats; i++)
        {
            seatNumber = monthlyTicketData[date].getFlight(direction).getFirstFreeSeat();
            monthlyTicketData[date].getFlight(direction).seats[seatNumber].client= (AID) client.clone() ;
        }
        if (seatNumber == -1)
        	return false;
        else
        	return true;

    }

    public boolean isTicketSoldToClient(AID client, Ticket ticket)
    {
        DailyTicketData sale = monthlyTicketData[ticket.getTicketDate()];
        boolean direction = ticket.getTicketDirection().getValue();
        if(sale.getFlight(direction).getNumberOfSeatsSoldToClient(client)== ticket.getTicketSeats() )
        	return true;
        else
        	return false;
    }

    public void cancelTicketSold(AID client, Ticket ticket)
    {
        boolean direction = ticket.getTicketDirection().getValue();
        monthlyTicketData[ticket.getTicketDate()].getFlight(direction).cancelAllSeatsSoldToClient(client);
    }

}